﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Advertise
{
    public partial class Admin : AdminPage
    {
        private Advertise curAdvertise = new Advertise();

        private string classno = "";
        private int pageSize = 15;
        public string clslist = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            classno = Request.QueryString["clsno"] == null ? "" : Request.QueryString["clsno"].Trim();
            if (!IsPostBack)
            {

                try
                {
                    switch (Request["t"])
                    {
                        case "delete":
                            Delete(Convert.ToInt32(Request["id"]));
                            break;
                    }

                    Repeater_admin_bind();
                    DdlMenu_bind();
                }
                catch (Exception ex) { Response.Redirect("../Message.aspx?message=绑定异常！" + ex.Message + "&z=no"); }
            }
        }
        #region 类别数据绑定
        void DdlMenu_bind()
        {
            string seleClslist = "";

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("所有分类", ""));
            int tCount = 1;
            ListItem li = new ListItem("图片广告", "0");
            DdlMenu.Items.Add(li);
            li = new ListItem("FLASH广告", "1");
            DdlMenu.Items.Add(li);
            li = new ListItem("幻灯片广告", "3");
            DdlMenu.Items.Add(li);
            li = new ListItem("对联广告", "4");
            DdlMenu.Items.Add(li);
            li = new ListItem("漂浮广告", "5");
            DdlMenu.Items.Add(li);
            li = new ListItem("头部大图", "6");
            DdlMenu.Items.Add(li);
            DdlMenu.SelectedIndex = tCount;

            //栏目
            DataTable dt = new Channel().GetOrderList(seleClslist);
            this.ddcolum.Items.Clear();
            this.ddcolum.Items.Add(new ListItem("所有栏目", ""));
            foreach (DataRow dr in dt.Rows)
            {
                int ClassTj = Convert.ToInt32(dr["clslistlen"]);
                string tmpClslist = dr["clslist"].ToString().Trim();
                string showfile = dr["showfile"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();
                if (ClassTj != 1)
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(ClassTj - 1, "　") + ClassName;
                }
                ListItem li1 = new ListItem(ClassName, tmpClslist);
                if (showfile == "")
                {
                    li.Attributes.Add("style", "background-color:#ffdfe2;");
                }
                this.ddcolum.Items.Add(li1);
            }
            this.ddcolum.SelectedValue = clslist;
        }
        #endregion
        /// <summary>
        /// 获取类别
        /// </summary>
        /// <returns></returns>
        public string getType(object type)
        {
            try
            {
                int tp = int.Parse(type.ToString());
                switch (tp)
                {
                    case 0:
                        return "图片广告";
                    case 1:
                        return "FLASH广告";
                    case 2:
                        return "幻灯片广告";
                    case 4:
                        return "顶部大图";
                    case 3:
                        return "漂浮广告";
                    default:
                        return "未知类别";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                return null;
            }
        }

        #region 数据绑定
        void Repeater_admin_bind()
        {
            string gopage = (Request.QueryString["page"] == null) ? "0" : Request.QueryString["page"].Trim();
            int page = Convert.ToInt32(gopage);

            int recordCount = 0;
            DataTable dt = curAdvertise.GetList(pageSize, (page - 1), false, false, out recordCount);
            litPager.Text = PlugNT.Common.Pager.Pagination(recordCount, pageSize, page, "Admin.aspx", GetBindUrl(), "", true, true);

            Repeater_admin.DataSource = dt;
            Repeater_admin.DataBind();


        }
        #endregion

        #region 显示数据处理
        protected void Repeater_admin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label Label_clsname = (Label)e.Item.FindControl("Label_clsname");
                DataRowView drv = (DataRowView)e.Item.DataItem;
                // Label_clsname.Text = curFriendLinkClass.GetTitle(drv["clsno"].ToString()); 
            }
        }

        public string refs(string name)
        {

            return "<script src=\"/js/Advertise/" + name + "\" type=\"text/javascript\"></script>";
        }

        #endregion


        private string GetBindUrl()
        {
            return "clsno=" + classno + "&page";
        }

        //批量删除
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)Repeater_admin.Items[i].FindControl("hidid")).Value.Trim());
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    if (curAdvertise.Delete(id))
                    {
                        nub += 1;
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");

        }

        private bool Delete(int id)
        {
            return curAdvertise.Delete(id);
        }
    }
}
